package com.graduation.springbootinit.controller;

import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.model.entity.Book;
import com.graduation.springbootinit.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 书籍接口
 */
@RestController
@RequestMapping("/api/book")
public class BookController {

    @Resource
    private BookService bookService;

    /**
     * 创建书籍
     * @param book 书籍信息
     * @return 书籍id
     */
    @PostMapping("/add")
    public BaseResponse<Long> addBook(@RequestBody @Valid Book book) {
        if (book == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long bookId = bookService.addBook(book);
        return ResultUtils.success(bookId);
    }

    /**
     * 删除书籍
     * @param id 书籍id
     * @return 是否成功
     */
    @PostMapping("/delete/{id}")
    public BaseResponse<Boolean> deleteBook(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = bookService.deleteBook(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新书籍
     * @param book 书籍信息
     * @return 是否成功
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateBook(@RequestBody @Valid Book book) {
        if (book == null || book.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = bookService.updateBook(book);
        return ResultUtils.success(result);
    }

    /**
     * 根据id获取书籍
     * @param id 书籍id
     * @return 书籍信息
     */
    @GetMapping("/get/{id}")
    public BaseResponse<Book> getBookById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Book book = bookService.getBookById(id);
        return ResultUtils.success(book);
    }

    /**
     * 获取所有书籍
     * @return 书籍列表
     */
    @GetMapping("/list")
    public BaseResponse<List<Book>> listBooks() {
        List<Book> bookList = bookService.listBooks();
        return ResultUtils.success(bookList);
    }

    /**
     * 获取推荐书籍列表
     * @return 推荐书籍列表
     */
    @GetMapping("/recommend")
    public BaseResponse<List<Book>> listRecommendBooks() {
        List<Book> bookList = bookService.listRecommendBooks();
        return ResultUtils.success(bookList);
    }

    /**
     * 批量添加书籍
     * @param bookList 书籍列表
     * @return 是否成功
     */
    @PostMapping("/batch/add")
    public BaseResponse<Boolean> batchAddBooks(@RequestBody List<Book> bookList) {
        if (bookList == null || bookList.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = bookService.batchAddBooks(bookList);
        return ResultUtils.success(result);
    }
}